﻿using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Transactions;
using Newtonsoft.Json;
using System.Reflection;
using System.Data;
using T.FAS.Runtime.Base.Entity;
using T.ServiceComponent.DAC.Service;
using T.ServiceComponent.DataAccessEx.Core;

namespace T.FAS.Runtime.Base.Dao
{
    public class ChartInstanceApplyDao : DaoService<ChartInstanceApply>
    {
        public override string TableName => "FAS_ChartInstanceApply";
        public override string IDColumnName => "TargetID";
        public ChartInstanceApplyDao() : base()
        {
            DBName = "TFAS";
        }
        public override List<string> ExceptColumnName => new List<string> { "ChartLib", "Code", "Name", "ChartLibItemID", "DataSourceID", "DemoData", "ChartContent" };
        public override Dictionary<string, object> GetDataMapping(ChartInstanceApply data)
        {
            var datas = base.GetDataMapping(data);
            datas.Remove("TargetType");
            datas.Add("TargetType", (int)(data.TargetType));
            return datas;
        }
        public override ChartInstanceApply ConvertToObject(Dictionary<string, PropertyInfo> columns, DataRow row)
        {
            var myCol = new Dictionary<string, PropertyInfo>(base.Columns);
            myCol.Remove("TargetType");
            var data = base.ConvertToObject(myCol, row);
            if (row["TargetType"] != DBNull.Value)
                data.TargetType = (TargetType)Enum.Parse(typeof(TargetType),
                                                    Convert.ToString(row["TargetType"]));
            return data;
        }
        /// <summary>
        /// 查询图表库
        /// </summary>
        /// <returns></returns>
        public List<string> GetChartInstanceApplyIDs(TargetType targetType, string targetID)
        {
            List<string> result = new List<string>();
            List<IDbDataParameter> paramList = new List<IDbDataParameter>();
            paramList.Add(DB.MakeInParam("@TargetType", TDbDataType.Int, (int)targetType));
            paramList.Add(DB.MakeInParam("@TargetID", TDbDataType.VarChar, targetID));
            string sql = @"select apply.ID
            from FAS_ChartInstance instance join FAS_ChartInstanceApply apply on instance.ID = apply.ChartInstanceID
            where TargetType = @TargetType and TargetID = @TargetID";
            var ds = DB.ExecuteDataSet(sql, paramList.ToArray());
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    var dr = ds.Tables[0].Rows[i];
                    result.Add(Convert.ToString(dr["ID"]));
                }
            }
            return result;
        }
        /// <summary>
        /// 查询图表库
        /// </summary>
        /// <returns></returns>
        public List<ChartInstanceApply> GetChartInstanceApplys(TargetType targetType, string targetID)
        {
            List<ChartInstanceApply> result = new List<ChartInstanceApply>();
            List<IDbDataParameter> paramList = new List<IDbDataParameter>();
            paramList.Add(DB.MakeInParam("@TargetType", TDbDataType.Int, (int)targetType));
            paramList.Add(DB.MakeInParam("@TargetID", TDbDataType.VarChar, targetID));
            string sql = @"select apply.ID,ChartInstanceID,TargetID,TargetType,
            instance.Code,instance.Name,instance.ChartLibItemID,
            instance.DataSourceID,instance.DataLoadType,instance.DemoData,instance.ChartContent
            from FAS_ChartInstance instance join FAS_ChartInstanceApply apply on instance.ID = apply.ChartInstanceID
            where TargetType = @TargetType and TargetID = @TargetID order by SortOrder";
            var ds = DB.ExecuteDataSet(sql, paramList.ToArray());
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    var dr = ds.Tables[0].Rows[i];
                    var apply = new ChartInstanceApply();
                    apply.ID = Convert.ToString(dr["ID"]);
                    apply.ChartInstanceID = Convert.ToString(dr["ChartInstanceID"]);
                    apply.TargetID = Convert.ToString(dr["TargetID"]);
                    if (dr["TargetType"] != DBNull.Value)
                        apply.TargetType = (TargetType)Enum.Parse(typeof(TargetType),
                                                            Convert.ToString(dr["TargetType"]));
                    apply.Code = Convert.ToString(dr["Code"]);
                    apply.Name = Convert.ToString(dr["Name"]);
                    apply.ChartLibItemID = Convert.ToString(dr["ChartLibItemID"]);
                    apply.DataSourceID = Convert.ToString(dr["DataSourceID"]);
                    apply.DemoData = Convert.ToString(dr["DemoData"]);
                    apply.ChartContent = Convert.ToString(dr["ChartContent"]);
                    result.Add(apply);
                }
            }
            return result;
        }
    }
}
